package com.google.gsaguide.fragments;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.RelativeLayout;

import com.google.gsaguide.DAO.AmbassadorDAO;
import com.google.gsaguide.MainActivity;
import com.google.gsaguide.R;
import com.google.gsaguide.adapters.AmbassadorsListAdapter;
import com.google.gsaguide.gplusbridge.GPlusRequestHelper;
import com.google.gsaguide.gplusbridge.OnGPlusRequestListener;
import com.google.gsaguide.gsa.Ambassador;

import org.json.JSONObject;

import java.util.List;

public class AmbassadorsFragment extends Fragment implements OnGPlusRequestListener, AdapterView.OnItemClickListener {

    // TODO: Need to optimize and comment this code.

    private final String DEBUG_TAG = AmbassadorsFragment.class.getSimpleName();
    private GPlusRequestHelper mGPlusAsyncConn;
    private RelativeLayout mLoading;
    private Context mContext;
    private ListView mListView;
    private AmbassadorsListAdapter mAdapter;

    private AmbassadorDAO mAmbDB;
    private List<Ambassador> mAmbList;

    public AmbassadorsFragment() {
    }

    public static AmbassadorsFragment newInstance() {
        AmbassadorsFragment fragment = new AmbassadorsFragment();

        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_ambassadors, container, false);

        mContext = getActivity().getApplicationContext();
        mAmbDB = AmbassadorDAO.getInstance(mContext);
        mAmbList = mAmbDB.getAll();

        mLoading = (RelativeLayout) view.findViewById(R.id.ambassadorsLoading);

        mListView = (ListView) view.findViewById(R.id.ambassadorList);
        mListView.setOnItemClickListener(this);
        mAdapter = new AmbassadorsListAdapter(getActivity().getLayoutInflater(), mContext);
        mListView.setAdapter(mAdapter);

        mLoading.setVisibility(View.VISIBLE);
        mListView.setVisibility(View.INVISIBLE);

        mGPlusAsyncConn = new GPlusRequestHelper(mContext, this);

        for (Ambassador amb : mAmbList) {
            if (amb.getPhotoURL() == null) {
                mGPlusAsyncConn.getInfo(amb.getId());
            } else {
                mAdapter.addItem(amb);
                dismissProgressBar();
            }
        }
        return view;
    }

    public void dismissProgressBar() {
        if (mLoading.getVisibility() == View.VISIBLE) {
            mLoading.setVisibility(View.INVISIBLE);
            mListView.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        ((MainActivity) activity).onSectionAttached(MainActivity.AMBASSADORS_FRAGMENT);
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mGPlusAsyncConn.cancelAllPedingRequests();
    }


    @Override
    public void onPostExecute(JSONObject response, int errorCode) {
        if (errorCode == GPlusRequestHelper.NO_ERROR) {
            try {
                for (Ambassador amb : mAmbList) {
                    if (amb.getId().equals(response.getString("id"))) {
                        amb.setPhotoURL(response.getJSONObject("image").getString("url").replace("sz=50", "sz=200"));
                        mAmbDB.update(amb);
                        mAdapter.addItem(amb);
                    }
                    dismissProgressBar();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else {
            Log.w(DEBUG_TAG, "Error " + errorCode);
        }
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(mAdapter.getItem(position).getProfileURL())));
    }
}
